# normalize values in to 0..1 cube with
# x: -1 left           to     1 right
# y: -1 towards/up     to     1 away/down
# z: -1 gravity/flat   to     1 upside down


# the 0 point, it typically slighty off
_x0, _y0, _z0 = -0.06, -0.20, 0
# to get the values for you device lay it flat on a table
# and enter above the values you see for x and y.
# z should represent earth gravitation: 10 on android

# to reduce flicker in the 0 position for x and y 
# just ignore some values around 0.
_ignore = 0.1

def _androidg1zero(block):
    for entry in block:
        entry["x"] = entry["x"]-_x0
        entry["y"] = entry["y"]-_y0
        entry["z"] = entry["z"]-_z0
        for c in "xy":
            if -_ignore < entry[c] < _ignore:
                entry[c] = 0
            else: # over threshold, cut out ignored bit
                if entry[c] < 0:
                    entry[c] += _ignore
                else:
                    entry[c] -= _ignore
    return block


scale = { "x" : -1./10, "y" : -1./10, "z": -1./10 }
def _androidg1normalize(block):
    """normalize to roughly -1..1 cube on the basis of scale.
    Note that the values can be outside that range if there is
    more than earth gravitation (shake the phone).
    """
    for entry in block:
        for k in entry:
            if k in scale:
                entry[k] = entry[k]*scale[k] # in place edit fine
    return block # MUST return anyway


def androidg1normalize(block):
    return _androidg1normalize(_androidg1zero(block))
